package com.rd.p2p.app.additional.redEnvelope;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.RedEnvelopeService;
import com.qdlc.p2p.biz.service.SystemConfigService;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.service.UserRedEnvelopeService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.SystemConfig;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.UserRedEnvelopeModel;

/**
 * 红包
 * 
 * @author ylx
 * @version 2.0
 * @date 2015年5月19日 上午11:16:47
 */
public class AppUserRedEnvelopeAction extends BaseAction<UserRedEnvelopeModel>
		implements ModelDriven<UserRedEnvelopeModel> {

	@Resource
	private UserRedEnvelopeService userRedEnvelopeService;
	@Resource
	private SystemConfigService systemConfigService;
	@Resource
	private UserIdentifyService userIdentifyService;
	@Resource
	private RedEnvelopeService redEnvelopeService;

	private UserRedEnvelopeModel model = new UserRedEnvelopeModel();

	@Override
	public UserRedEnvelopeModel getModel() {
		return model;
	}

	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * 我的红包数据
	 * 
	 * @throws IOException
	 */
	@Action("/app/user/redEnvelopeJSON")
	public void availableRedEnvelopeJSON() throws IOException {
		User user = getSessionUser();
		data = new HashMap<String, Object>();
		if (user == null) {
			printWebResCodeAndMsgResult("-1","用户尚未登陆!");
			return;
		}
		// checkInvestUser();
		model.setUserId(user.getUserId());
		PageDataList<UserRedEnvelopeModel> pagaDataList = userRedEnvelopeService.list(model);
		int total = pagaDataList.getPage().getTotal();// 总记录数
		data.put("total", total);
		data.put("data", pagaDataList);
		data.put("resCode", "1");
		printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 查看用户是否有可用的红包
	 * 
	 * @throws Exception
	 */
	@Action("/app/user/checkEnvelopeUseable")
	public void checkEnvelopeUseable() throws IOException {
		User user = getSessionUser();
		data = new HashMap<String, Object>();
		if (user == null) {
			printWebResCodeAndMsgResult("-1","用户尚未登陆!");
			return;
		}
		List<UserRedEnvelopeModel> list = userRedEnvelopeService.useableList(user.getUserId());
		if(list.size()>0){
			data.put("resCode", "1");
			data.put("resMsg",MessageUtil.getMessage("MS100002004"));
		}
		else{
			data.put("resCode", "0");
			data.put("resMsg", MessageUtil.getMessage("MF100002006"));
		}
		printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 查看投标使用红包规则
	 * 
	 * @throws Exception
	 */
	@Action("/app/user/getRedEnvelopeRule")
	public void getRedEnvelopeRule() throws IOException {
		String redPacketsRule = SystemConfigHelper.getValue(SystemConfigHelper.Nid.RED_PACKETS_RULE);
	       if(StringUtils.isNotEmpty(redPacketsRule)){
	    	   data.put("resCode", "1");
	    	   data.put("redPacketsRule", redPacketsRule);
	       }
	       else{
	    	   data.put("resCode", "0");
	    	   data.put("redPacketsRule", "暂无投标使用红包规则");
	       }
	      printWebJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 获取用户可用红包列表
	 * 
	 * @throws Exception
	 */
	@Action(value="/app/redEnvelope/useable", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void useable() throws Exception {
		User user = getSessionUser();
		List<UserRedEnvelopeModel> list = userRedEnvelopeService.useableList(user.getUserId());
		data.put("rows", list);
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 初始化用户红包
	 * 
	 * @throws Exception
	 */
	@Action(value="/app/redEnvelope/initUseable", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void initUseable() throws Exception {
		User user = getSessionUser();
		// 用户可用红包个数
		int count = userRedEnvelopeService.userableCount(user.getUserId());
		data.put("rdEnvelopeCount", count);
		
		// 红包最大占投资金额比例
		data.put("tenderRedpackageRate", SystemConfigHelper.getValue(SystemConfigHelper.Nid.USER_TENDER_REDPACKAGE_RATE));
		printJson(getStringOfJpaObj(data));
	}

}
